#pragma once

#include "IXFileReader.h"
#include "IXBaseObj.h"
#include "IXFileMapBuffer.h"
#include "XBaseObjImpl.h"

#include <map>

class CXFileReaderImpl : public CXBaseObjImpl<IXFileReader>
{
    typedef CXBaseObjImpl<IXFileReader> superclass;
    DEF_SKIN_CLASS_SINGLE_INTF(CXFileReaderImpl,
        superclass,
        gs_strGuidXSkin,
        IXFileReader,
        0);
public:
    CXFileReaderImpl();
    virtual ~CXFileReaderImpl();

public:
    // IXFileReader
    virtual HRESULT STDMETHODCALLTYPE   put_XNC(IN LPCTSTR szXNC);
    virtual HRESULT STDMETHODCALLTYPE   LoadObject(IN LPCTSTR szObjectName, OUT IXFileMapBuffer** pBuffer);
    virtual HRESULT STDMETHODCALLTYPE   EnumObject(IN OUT DWORD pos, OUT BSTR* bstrObjectName);
    virtual DWORD   STDMETHODCALLTYPE   GetObjectCount();
    virtual BOOL    STDMETHODCALLTYPE   IsFileExists(IN LPCTSTR szFilePath);
    virtual BOOL    STDMETHODCALLTYPE   IsFolderExists(IN LPCTSTR szFolderPath);

    // IXBaseObj
    virtual void    STDMETHODCALLTYPE   OnFinalRelease();

protected:
    void    CloseXFile();
    BOOL    ParseRealFile();
    BOOL    IsValid();

    BOOL    ParseRealFS(LPCTSTR szPath);
    BOOL    ParseXFile(IN LPCTSTR szRelativePath);

    HRESULT put_XFilePath(IN LPCTSTR szXFilePath, LPCTSTR szRelative);
    HRESULT put_RealFS(IN LPCTSTR szRealFSPath, LPCTSTR szRelative);

    BOOL    IsObjectExists(IN LPCTSTR szObjPath, IN BOOL bFolder);
protected:
    HANDLE  m_hXFile;
    HANDLE  m_hFileMapping;
    DWORD   m_dwXFileSize;

    struct stXObjectData
    {
        DWORD   dwFileLength;
        DWORD   dwFileOffset;
    };
    typedef std::map<XString , stXObjectData> XObjectDataMap;
    XObjectDataMap  m_XObjectDataMap;

    BOOL            m_bRealFS;
    XString         m_strRootPath;
    XString         m_strRelativePath;
};


